<template>
	<view class="button"><button class="button-add" v-if="isAddBtn" @click="onAddBtn">新增任务</button><button
			class="button-deduct" v-if="isAddBtn" @click="onScoreBtn">加分/扣分</button></view>

	<view class="container" v-if="isData">
		<!-- 导航栏 -->
		<!-- <clockScoll :navList="navList"></clockScoll> -->

		<!-- 打卡列表 -->
		<view class="list">
			<clockList :dataList="dataList" :isClockBtn="isClockBtn" :isUpdateBtn="isUpdateBtn"></clockList>
		</view>
	</view>
	<view class="container" v-else> 无关系数据</view>
	<uni-load-more :status="status" :content-text="contentText" @clickLoadMore="onLoadList()" />
	<affiche :title="showAffiche.affiche_title" :body="showAffiche.affiche_content" :time="showAffiche.create_time"
		:show="isShow" @change="changeShow"></affiche>
</template>

<script>
	import {
		onToken
	} from '@/utils/token.js'
	import store from '@/store/index.js'; //需要引入store
	import affiche from '@/components/affiche/affiche.vue';
	import {
		getUserInfo,
		babyClockTaskList,
		patriarchClockTaskList,
		publicAffiche
	} from '@/api/api.js'
	export default {
		components: {
			affiche
		},
		data() {
			return {
				isShow: store.state.isAfficheShow,
				listReq: {
					type: 1,
					pageInfo: {
						page: 1,
						size: 5,
					}
				},
				status: 'more',
				contentText: {
					contentdown: '点击查看更多',
				},
				isData: true,
				isClockBtn: true,
				isUpdateBtn: true,
				isAddBtn: true,
				navList: ['每日', '周末限定', '节日限定'],
				activeNavIndex: 0,
				dataList: [],
				showAffiche: {},
			}
		},
		onLoad() {
			onToken()
			this.loadAffiche()
			this.loadUserInfo()
		},
		onTabItemTap() {
			if (this.dataList.length > 0) {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}

		},

		methods: {
			changeShow(newShow) {
				// 接收子组件传递过来的新布尔值，更新父组件中的 isShow
				this.isShow = newShow;
				store.state.isAfficheShow = newShow
			},
			onAddBtn() {
				uni.redirectTo({
					url: '/pages/index/add/add'
				});
			},
			onScoreBtn() {
				uni.redirectTo({
					url: '/pages/index/scoreAdd'
				});
			},
			onLoadList() {
				this.status = 'loading'
				this.listReq.pageInfo.page += 1
				this.loadList()
			},
			loadAffiche() {
				publicAffiche().then(res => {
					console.log(res.data)
					if (res.data.code != 0) {
						uni.showToast({
							icon: "none",
							title: res.data.message
						})
					} else {
						this.showAffiche = res.data.data
					}
				})
			},
			loadUserInfo() {
				getUserInfo().then(res => {
					console.log(res.data)
					if (res.data.code != 0) {
						uni.showToast({
							icon: "none",
							title: res.data.message
						})
						uni.navigateTo({
							url: '/pages/login/login'
						});
					} else {
						store.state.avatarUrl = res.data.data.avatar_url
						store.state.nickName = res.data.data.nick_name
						store.state.gender = res.data.data.gender
						store.state.patriarchCode = res.data.data.patriarch_code
						store.state.userSid = res.data.data.user_sid
						store.state.province = res.data.data.province
						store.state.userType = res.data.data.user_type
						console.log(store.state)
						this.loadGlobalConfig()
						this.loadIdentity()
						this.loadList()
					}
				})
			},
			loadGlobalConfig() {
				if (store.state.userType == 1) {
					uni.setTabBarItem({
						index: 0,
						text: '任务配置',
						iconPath: '/static/images/tabbar/home.png',
						selectedIconPath: '/static/images/tabbar/home-h.png'
					})
					uni.setTabBarItem({
						index: 1,
						text: '商城配置',
						iconPath: '/static/images/tabbar/shop.png',
						selectedIconPath: '/static/images/tabbar/shop-h.png'
					})
					uni.setTabBarItem({
						index: 2,
						text: '打卡记录',
						iconPath: '/static/images/tabbar/list.png',
						selectedIconPath: '/static/images/tabbar/list-h.png'
					})
					uni.setTabBarItem({
						index: 3,
						text: '个人中心',
						iconPath: '/static/images/tabbar/user-1.png',
						selectedIconPath: '/static/images/tabbar/user-1-h.png'
					})
				}
				if (store.state.userType == 2) {
					uni.setTabBarItem({
						index: 0,
						text: '任务中心',
						iconPath: '/static/images/tabbar/home.png',
						selectedIconPath: '/static/images/tabbar/home-h.png'
					})
					uni.setTabBarItem({
						index: 1,
						text: '商城中心',
						iconPath: '/static/images/tabbar/trade.png',
						selectedIconPath: '/static/images/tabbar/trade-h.png'
					})
					uni.setTabBarItem({
						index: 2,
						text: '打卡记录',
						iconPath: '/static/images/tabbar/list.png',
						selectedIconPath: '/static/images/tabbar/list-h.png'
					})
					uni.setTabBarItem({
						index: 3,
						text: '个人中心',
						iconPath: '/static/images/tabbar/user-2.png',
						selectedIconPath: '/static/images/tabbar/user-2-h.png'
					})
				}
			},
			//身份不同，按钮不同
			loadIdentity() {
				if (store.state.userType == 1) {
					this.isAddBtn = true
					this.isClockBtn = false
					this.isUpdateBtn = true
				} else {
					this.isAddBtn = false
					this.isClockBtn = true
					this.isUpdateBtn = false
				}
			},

			//获取任务列表
			loadList() {
				if (store.state.userType == 1) {
					patriarchClockTaskList(this.listReq).then(res => {
						console.log(res.data)

						if (res.data.code != 0) {
							this.listReq.pageInfo.page -= 1
							this.status = 'noMore'
							uni.showToast({
								icon: "none",
								title: res.data.message
							})
						} else {
							var data = res.data.data
							let list = this.dataList
							this.dataList = list.concat(data.list)
							if (data.page_info.total_page - this.listReq.pageInfo.page <= 0) {
								this.status = 'noMore'
							} else {
								this.status = 'more'
							}
						}

					})
				} else {
					babyClockTaskList(this.listReq).then(res => {
						console.log(res.data)
						if (res.data.code != 0) {
							this.listReq.pageInfo.page -= 1
							uni.showToast({
								icon: "none",
								title: res.data.message
							})
							if (res.data.code == 50002) {
								this.isData = false
								this.status = 'noMore'
								uni.redirectTo({
									url: '/pages/user/user_relation'
								});
							}
						} else {
							var data = res.data.data
							let list = this.dataList
							this.dataList = list.concat(data.list)
							if (data.page_info.total_page - this.listReq.pageInfo.page <= 0) {
								this.status = 'noMore'
							} else {
								this.status = 'more'
							}
						}
					})
				}
			}
		}
	}
</script>
<style>
	.container {
		margin-top: 100rpx;
		display: flex;
		flex-direction: column;
		padding: 20upx;
	}

	.button {
		display: flex;

	}

	.button-add {
		position: absolute;
		margin-left: 100rpx;
	}

	.button-deduct {
		position: absolute;
		margin-left: 300rpx;
	}

	.list {
		width: 100%;
	}
</style>